package com.example.demo.services.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.example.demo.domain.po.Book;
import com.example.demo.domain.query.BookQuery;
import com.example.demo.domain.vo.BookVO;
import com.example.demo.domain.vo.FloorVO;
import com.example.demo.exception.MyException;
import com.example.demo.mapper.BookMapper;
import com.example.demo.services.BookServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookServicesImpl implements BookServices {
    @Autowired
    BookMapper bookMapper;

    @Override
    public List<BookVO> getAll() {
        List<BookVO> all = bookMapper.getAll();
        if(CollUtil.isEmpty(all)){
            throw new MyException("没有数据！");
        }
        return all;
    }

    @Override
    public BookVO get(int id) {
        BookVO bookVO = bookMapper.get(id);
        if(BeanUtil.isEmpty(bookVO)){
            throw new MyException("没有找到ID为"+id+"的数据");
        }
        return bookVO;
    }

    @Override
    public void edit(Book book) {
        int edit = bookMapper.edit(book);
        if(edit <= 0){
            throw new MyException("无法修改或者没有数据可修改！");
        }
    }

    @Override
    public void add(Book book) {
        int add = bookMapper.add(book);
        if(add <= 0){
            throw new MyException("添加失败");
        }
    }

    @Override
    public void del(int id) {
        int del = bookMapper.del(id);
        if(del<=0){
            throw new MyException("无法删除或者没有数据可以删除!");
        }
    }

    @Override
    public List<BookVO> fuzzySearch(BookQuery bookQuery) {
        List<BookVO> all = bookMapper.fuzzySearch(bookQuery);
        if(CollUtil.isEmpty(all)){
            throw new MyException("没有数据！请确认搜索词是否存在后重试！");
        }
        return all;
    }

    @Override
    public List<BookVO> autoSearch(String bookQuery) {
        List<BookVO> all = bookMapper.autoSearch(bookQuery);
        return all;
    }
}
